# -*- coding: utf-8 -*-
#
#    pyqtsozluk, qtsozluk and pysozluk mix dictionary application
#    Copyright (C) 2007  pyqtsozluk authors
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from PyQt4 import QtCore, QtGui
from gettext import translation
_ = translation('pyqtsozluk', fallback=True).ugettext

class mainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self)

        self.settings = QtCore.QSettings()
        self.resize(QtCore.QSize(QtCore.QRect(0,0,400,300).size()).expandedTo(self.minimumSizeHint()))
        self.setWindowTitle(_("PyQtSozluk"))
        self.setWindowIcon(QtGui.QIcon("/usr/share/pyqtsozluk/pyqtsozluktray.png"))

        self.centralwidget = QtGui.QWidget(self)
        self.centralwidget.setObjectName("centralwidget")

        self.gridlayout = QtGui.QGridLayout(self.centralwidget)
        self.gridlayout.setObjectName("gridlayout")

        self.lineEdit = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit.setObjectName("lineEdit")
        self.gridlayout.addWidget(self.lineEdit,1,1,1,1)

        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setObjectName("label")
        self.label.setText(_("Keyword"))
        self.gridlayout.addWidget(self.label,1,0,1,1)

        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText(_("&Search"))
        self.gridlayout.addWidget(self.pushButton,1,2,1,1)

        self.textBrowser = QtGui.QTextBrowser(self.centralwidget)
        self.textBrowser.setObjectName("textBrowser")
        self.textBrowser.setHtml("""<html><head><meta name="qrichtext" content="1" /><style type="text/css">\np, li { white-space: pre-wrap; }\n</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;">\n<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"></p></body></html>""")
        self.gridlayout.addWidget(self.textBrowser,0,0,1,3)
        self.setCentralWidget(self.centralwidget)

        self.menubar = QtGui.QMenuBar(self)
        self.menubar.setGeometry(QtCore.QRect(0,0,400,29))
        self.menubar.setObjectName("menubar")

        self.menuFile = QtGui.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        self.menuFile.setTitle(_("&File"))

        self.menuMode = QtGui.QMenu(self.menubar)
        self.menuMode.setObjectName("menuMode")
        self.menuMode.setTitle(_("&Mode"))
        self.setMenuBar(self.menubar)

        self.statusbar = QtGui.QStatusBar(self)
        self.statusbar.setObjectName("statusbar")
        self.setStatusBar(self.statusbar)

        self.actionOffline = QtGui.QAction(self)
        self.actionOffline.setCheckable(True)
        self.actionOffline.setChecked(True)
        self.actionOffline.setText(_("&Offline"))
        self.actionOffline.setShortcut(QtGui.QKeySequence(_("Ctrl+M")))
        self.actionOffline.setObjectName("actionOffline")

        self.actionSave = QtGui.QAction(self)
        self.actionSave.setText(_("&Save"))
        self.actionSave.setShortcut(QtGui.QKeySequence(_("Ctrl+S")))
        self.actionSave.setObjectName("actionSave")

        self.actionClose = QtGui.QAction(self)
        self.actionClose.setText(_("&Close"))
        self.actionClose.setShortcut(QtGui.QKeySequence(_("Alt+F4")))
        self.actionClose.setObjectName("actionClose")

        self.menuFile.addAction(self.actionSave)
        self.menuFile.addSeparator()
        self.menuFile.addAction(self.actionClose)
        self.menuMode.addAction(self.actionOffline)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuMode.menuAction())

        QtCore.QMetaObject.connectSlotsByName(self)

        self.show()

    def makeConnections(self, core):
        QtCore.QObject.connect(self.actionOffline,QtCore.SIGNAL("activated()"), self.toggleOffline)
        QtCore.QObject.connect(self.actionClose,QtCore.SIGNAL("activated()"), self.mainWindowClose)
        QtCore.QObject.connect(self.actionSave,QtCore.SIGNAL("activated()"), self.save)
        QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL("clicked()"), core.search)
        QtCore.QObject.connect(self.lineEdit,QtCore.SIGNAL("returnPressed()"), core.search)

    def toggleOffline(self):
        self.settings.setValue("search/offline", QtCore.QVariant(self.actionOffline.isChecked()))

    def save(self):
        if not self.textBrowser.toPlainText():
            QtGui.QMessageBox.warning(self,
            _("Warning"),
            _("Search a keyword first."))
        else:
            if self.settings.contains("lastsavelocation"):
                lastsaveloc = self.settings.value("lastsavelocation").toString()
            else:
                import os
                lastsaveloc = os.getenv("HOME")

            file_path = QtGui.QFileDialog.getSaveFileName(self, _("Save"),
                                                          lastsaveloc,
                                                         "Text-Html (*.txt *.html)")
            if file_path:
                if str(file_path).endswith(".html"):
                    description = self.textBrowser.toHtml()
                else:
                    description = self.textBrowser.toPlainText()
                file = open(str(file_path), "w")
                file.write(description + "\n")
                file.close()
                file_path.truncate(file_path.lastIndexOf("/"))
                self.settings.setValue("lastsavelocation", QtCore.QVariant(file_path))

    def mainWindowClose(self):
        self.settings.setValue("windowposition", QtCore.QVariant(self.pos()))
        self.textBrowser.clear()
        self.lineEdit.clear()
        self.close()
